package com.dutiantech.file.core;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import com.dutiantech.rambo.tool.basic.DTUidUtil;

public class TestMain {

	public static void main(String[] args) throws FileNotFoundException {

		try {
			FileManager fm = new NormalFTPInstance();
			boolean loginResult = fm.login("ftpuser", "1357924681", "192.168.9.39", 21);
			// boolean loginResult = fm.login("root", "V8xK+-zs,b8n5f5R", "123.206.182.46",
			// 22 ) ;
			System.out.println("登录结果：" + loginResult);
			if (loginResult) {
				String upFileName = "a.xls";
				fm.mkdir("test");//根目录下创建test文件夹，如果存在就直接cd进去
				testUp(fm, upFileName);
				testDown1(fm, upFileName);
				testDown2(fm, upFileName);
				fm.exit();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//上传测试
	private static void testUp(FileManager fm, String upFileName) throws UnsupportedEncodingException {
		String h = "<html xmlns:x='urn:schemas-microsoft-com:office:excel'><head><title>Excel</title></head><body><table border='1' style='font-size:13px;font-family:微软雅黑'><tr><td style='text-align:center' style='mso-number-format:\\@'><b>UUID</b></td><td style='text-align:center' style='mso-number-format:\\@'><b>日期时间</b></td><td style='text-align:center' style='mso-number-format:\\@'><b>随机数</b></td></tr><tr><td style='text-align:center' colspan='3' style='mso-number-format:\\@'>-----------------------------------------------------------</td></tr><tr><td style='text-align:center' style='mso-number-format:\\@'>BEDE828AC20744DD866564CBF23E33D5Y0QPAK1E</td><td style='text-align:center' style='mso-number-format:\\@'>2018-01-10 13:48:02.100</td><td style='text-align:center' style='mso-number-format:\\@'>7270836627</td></tr><tr><td style='text-align:center' style='mso-number-format:\\@'>B32210E3BBFD45578AFCF29A14DAD880XNQ2UL1B</td><td style='text-align:center' style='mso-number-format:\\@'>2018-01-10 13:48:02.100</td><td style='text-align:center' style='mso-number-format:\\@'>4989876058</td></tr><tr><td style='text-align:center' style='mso-number-format:\\@'>0851BA5A42A347C8AACFD5D7048DCFF1UMRCDRU3</td><td style='text-align:center' style='mso-number-format:\\@'>2018-01-10 13:48:02.100</td><td style='text-align:center' style='mso-number-format:\\@'>8121509372</td></tr><tr><td style='text-align:center' style='mso-number-format:\\@'>22A89EE80F864026B857113274D9D64EU95BFPZF</td><td style='text-align:center' style='mso-number-format:\\@'>2018-01-10 13:48:02.100</td><td style='text-align:center' style='mso-number-format:\\@'>9016657949</td></tr><tr><td style='text-align:center' style='mso-number-format:\\@'>9E64E6AFF87545AC957A7B62FAA52E07L4AB4UQZ</td><td style='text-align:center' style='mso-number-format:\\@'>2018-01-10 13:48:02.100</td><td style='text-align:center' style='mso-number-format:\\@'>4868528545</td></tr><tr><td style='text-align:center' style='mso-number-format:\\@'>D3FF944CAB9746C7A7B20462180A7AE71TM5OPPS</td><td style='text-align:center' style='mso-number-format:\\@'>2018-01-10 13:48:02.100</td><td style='text-align:center' style='mso-number-format:\\@'>4943490985</td></tr><tr><td style='text-align:center' style='mso-number-format:\\@'>759B450B098448FD913D270D152462A2Y4PCRWUR</td><td style='text-align:center' style='mso-number-format:\\@'>2018-01-10 13:48:02.100</td><td style='text-align:center' style='mso-number-format:\\@'>3803357313</td></tr><tr><td style='text-align:center' style='mso-number-format:\\@'>4D03D0EF90DE41FAA267F1C0B6FC567B1SA1WJ0D</td><td style='text-align:center' style='mso-number-format:\\@'>2018-01-10 13:48:02.100</td><td style='text-align:center' style='mso-number-format:\\@'>5665521837</td></tr><tr><td style='text-align:center' style='mso-number-format:\\@'>2C4DB390F0A54EA3A34FF9201756C18DMDLBMBFR</td><td style='text-align:center' style='mso-number-format:\\@'>2018-01-10 13:48:02.100</td><td style='text-align:center' style='mso-number-format:\\@'>1836344149</td></tr><tr><td style='text-align:center' style='mso-number-format:\\@'>DB85E6F31BEA4D4E923576BE3200C85AS0OHIZ5A</td><td style='text-align:center' style='mso-number-format:\\@'>2018-01-10 13:48:02.100</td><td style='text-align:center' style='mso-number-format:\\@'>2920782548</td></tr></table></body></html>";
		InputStream is = new ByteArrayInputStream(h.getBytes("UTF-8"));
		boolean result = fm.put(upFileName, is);
		System.out.println("上传结果：" + result);
	}

	//下载测试
	private static void testDown1(FileManager fm, String downFileName) throws IOException {
		boolean result = fm.download("D:\\", "666.xls", downFileName);
		System.out.println("下载结果：" + result + "； 位置：" + "D:\\666.xls");
	}
	
	//下载测试
	private static void testDown2(FileManager fm, String downFileName) throws IOException {
		InputStream input = fm.download(downFileName);
		String filePath = "D:\\"+DTUidUtil.genUID_19()+".xls";
		writeToLocal(filePath, input);
		System.out.println("获取inputStream手动写入完成：" + filePath);
	}

	private static void writeToLocal(String filePath, InputStream input) throws IOException {
		FileOutputStream fos = new FileOutputStream(filePath);
		byte[] b = new byte[1024];
		while ((input.read(b)) != -1) {
			fos.write(b);
		}
		input.close();
		fos.close();
	}

}
